<?php

class controller_technic extends controller_theme
{

	public function __construct()
	{
		parent::__construct();
	}


	public function listing()
	{
		$key = get('key', null);
		$category_id = get('category_id', 0, 'int');

		$model = bone::get_model('technic');
		$template = bone::get_template('technic.listing', APP_DIR);
		$template->set_title('技术文档列表');

		$limit = 30;
		$pagination = $template->get_pagination();
		$pagination->set_limit( $limit );
		$pagination->set_total( $model->get_technic_sum($key, $category_id) );
		$pagination->set_page(get('page', 1, 'int'));
		$pagination->set_url('./?controller=technic&task=listing&category_id='.$category_id);
		
		$technics = $model->get_technics($key, $category_id, $pagination->get_offset(), $limit);
		$categories = $model->get_categories();

		$template->set('category_id', $category_id);
		$template->set('technics', $technics);
		$template->set('categories', $categories);
		$template->set_data($this->get_data());
		$template->display();
	}


	public function edit()
	{
		$technic_id = get('technic_id', 0, 'int');

		$technic = bone::get_table('technic');
		$technic->load($technic_id);

		$model = bone::get_model('technic');
		$categories = $model->get_categories();

		$template = bone::get_template('technic.edit', APP_DIR);
		if($technic_id)
			$template->set_title('编辑技术文档');
		else
			$template->set_title('添加技术文档');

		$template->set('technic', $technic);
		$template->set('categories', $categories);
		$template->set_data($this->get_data());
		$template->display();
	}


	public function edit_save()
	{
		$id = post('id', 0, 'int');
		$my = bone::get_user();

		$technic = bone::get_table('technic');
		if($id) $technic->load($id);
		$technic->bind($_POST);
		$technic->create_time = time();
		$technic->save();

		if( $id ){$msg = '修改技术文档成功';}else{$msg = '添加技术文档成功';}
		$this->redirect('./?controller=technic&task=listing&category_id='.$technic->category_id, $msg);
	}


	public function delete()
	{
		$technic_id = get('technic_id', 0, 'int');
		if(!$technic_id) exit;

		$technic = bone::get_table('technic');
		$technic->delete( $technic_id );
		
		$this->redirect('./?controller=technic&task=listing', '删除技术文档成功');
	}



	
}
?>